package com.dhcc.core.modules.system.service.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.baomidou.mybatisplus.plugins.Page;
import com.dhcc.core.framework.base.service.impl.BaseServiceImpl;
import com.dhcc.core.framework.result.node.ZTreeNode;
import com.dhcc.core.framework.util.ArrayUtil;
import com.dhcc.core.framework.util.CommonUtil;
import com.dhcc.core.modules.system.dao.ShortcutMapper;
import com.dhcc.core.modules.system.entity.Shortcut;
import com.dhcc.core.modules.system.service.IShortcutService;


/**
 * 快捷方式表表数据服务层接口
 * 
 * @ClassName: ShortcutServiceImpl
 * @Description: TODO
 * @author lf
 * @date 2018-09-26 17:24:23
 */
@Service("shortcutService")
public class ShortcutServiceImpl extends BaseServiceImpl<ShortcutMapper, Shortcut> implements IShortcutService {

	@Override
	public List<Shortcut> list(Page<Shortcut> page, Map<String, Object> map) {
		return this.baseMapper.list(page, map);
	}
	
	@Override
	public List<Shortcut> searchByUserId(Long userId) {
		Map<String,Object> params=new HashMap<String, Object>();
        params.put("createBy", userId);
		return this.baseMapper.list(params);
	}
    
    @Override
    @Transactional(rollbackFor = Exception.class)
    public List<ZTreeNode> resourceTreeListByRoleId(Integer RESOURCE_GROUP,Long createBy){
    	//获取用户roleids，将roleid添加到查询条件
    	List<String> role_Ids=this.baseMapper.selectRoleIdsByUserId(createBy);
    	String[] roleidsString;
    	List<Long> principalIds = new ArrayList<Long>();
    	for(String str:role_Ids){
    		if(str.length()!=0){
    			roleidsString =str.split(",");
    	    	for(int i=0;i<roleidsString.length;i++){    		
    	    		principalIds.add(i, Long.parseLong(roleidsString[i].substring(1, roleidsString[i].length()-1)));
    	    	}
    		}
    	}
    	
    	//添加用户的部门id到查询条件
    	List<Long> deptIds=this.baseMapper.selectDeptIdsByUserId(createBy);
    	for(Long deptId:deptIds){
    		principalIds.add(deptId);
    	}
    	
    	//添加角色everyone的id到查询条件
    	principalIds.add((long) 9999);
    	
    	//添加用户id到查询条件
    	principalIds.add(createBy);
    	
    	//获取该用户的的resourceids
    	List<Long> resourceIds = this.baseMapper.getResourceIdsByPrincipalIds(principalIds);
    	
    	//生成快捷方式树
    	if (CommonUtil.isEmpty(resourceIds)) {
            List<ZTreeNode> roleTreeList = new ArrayList<ZTreeNode>();
            return roleTreeList;
        } else {
            List<ZTreeNode> resourceTreeListByResourceIds = this.baseMapper.resourceTreeListByResourceIds(RESOURCE_GROUP,
                    resourceIds,createBy);
            for (int i=0;i<resourceTreeListByResourceIds.size();i++){ 
            	String url= this.baseMapper.selectUrlById(resourceTreeListByResourceIds.get(i).getId());
            	if("".equals(url)){//将父节点的复选框取消
            		resourceTreeListByResourceIds.get(i).setNocheck(true);
            	}
            }
            return resourceTreeListByResourceIds;
        }
    }
    
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void setShortcut(String ids,Long createBy) {
    	int weight=1;
    	// 删除该用户所有的快捷方式
    	this.baseMapper.deleteByUserID(createBy);
  
        // 添加新的快捷方式
    	if(CommonUtil.isNotEmpty(ids)){
    		for (Long id : ArrayUtil.stringToLongArray(ids, ",")) {
    			Shortcut shortcut=new Shortcut();
    			shortcut.setResourceId(id);
    			shortcut.setCreateBy(createBy);
    			shortcut.setWeight(weight++);
    			shortcut.insert();
    		}
    	}
    }
}